package handler

import (
	"area/model"
	area "area/proto"
	"area/utils"
	"context"
)

type Area struct{}

// Return a new handler
func New() *Area {
	return &Area{}
}

func (e *Area) MicroGetArea(ctx context.Context, req *area.Request, rsp *area.Response) error {
	//获取数据并返回给调用者
	areas, err := model.GetArea()
	if err != nil {
		rsp.Errno = utils.RECODE_DBERR
		rsp.Errmsg = utils.RecodeText(utils.RECODE_DBERR)
		return err
	}
	rsp.Errno = utils.RECODE_OK
	rsp.Errmsg = utils.RecodeText(utils.RECODE_OK)

	for _, v := range areas {
		var areaInfo area.AreaInfo
		areaInfo.Aid = int32(v.Id)
		areaInfo.Aname = v.Name

		rsp.Data = append(rsp.Data, &areaInfo)
	}

	return nil
}
